package com.tiantianyuegou.activity;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.tiantianyuegou.R;
import com.tiantianyuegou.http.ApiDefine;
import com.tiantianyuegou.model.BankModel;
import com.tiantianyuegou.util.Tools;
import com.tiantianyuegou.view.widget.OnWheelChangedListener;
import com.tiantianyuegou.view.widget.WheelView;
import com.tiantianyuegou.view.widget.adapters.ArrayWheelAdapter;
import com.trouble.framework.pay.PayType;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 添加银行卡
 */
public class AddBankActivity extends BaseActivity implements View.OnClickListener,  TextWatcher {

    private ImageView img_title;
    private TextView text_title, add_bank_name;
    private EditText edit_card_id;
    private Button btn_next;

    private RelativeLayout relative_bank;
    private Dialog dialog;
    private String TAG = "AddBankActivity";
    private String cardNum;
    private String[] bankList;
    private boolean bank = false;
    private List<BankModel> bankModelList;
    private ArrayWheelAdapter mAdapter;
    private String twId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_bank);
        loadBankList();
        init();
        Tools.changeSystemBar(this);
    }

    @Override
    public void findViewByIds() {

    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {

    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    private void init() {
        text_title = (TextView) findViewById(R.id.text_title);
        img_title = (ImageView) findViewById(R.id.img_title);
        add_bank_name = (TextView) findViewById(R.id.add_bank_name);
        text_title.setText(R.string.add_title);
        img_title.setOnClickListener(this);

        edit_card_id = (EditText) findViewById(R.id.edit_card_id);
        btn_next = (Button) findViewById(R.id.btn_next);
        relative_bank = (RelativeLayout) findViewById(R.id.relative_bank);

        btn_next.setOnClickListener(this);
        relative_bank.setOnClickListener(this);
        edit_card_id.addTextChangedListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_title:
                finish();
                break;

            case R.id.relative_bank:

                headDialog();
                break;

            case R.id.btn_next:
                String number = edit_card_id.getText().toString();
                if (bank && !TextUtils.isEmpty(number) && (number.length() >= 12 && number.length() <= 19)) {
                    setResult(RESULT_OK);
                    Intent btn_ntxt = new Intent();
                    btn_ntxt.putExtra("number", number);
                    btn_ntxt.putExtra("twId", twId);
                    btn_ntxt.setClass(AddBankActivity.this, AddCardActivity.class);
                    startActivityForResult(btn_ntxt, 1);
                } else {
                    showMsg("请确认您已选择银行，并且输入了正确的的银行卡号");
                }
                break;
            default:
                break;
        }
    }

    private void headDialog() {
//        TODO 查找不到wheel_bank
        View view = getLayoutInflater().inflate(R.layout.dialog_bank_list, null);
        dialog = new Dialog(this, R.style.transprentFrameWindowStyle);
        dialog.setContentView(view, new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT));
        Window window = dialog.getWindow();
        window.setWindowAnimations(R.style.main_menu_animstyle);
        WindowManager.LayoutParams wl = window.getAttributes();
        wl.x = 0;
        wl.y = getWindowManager().getDefaultDisplay().getHeight();
        wl.width = ViewGroup.LayoutParams.MATCH_PARENT;
        wl.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        dialog.onWindowAttributesChanged(wl);
        dialog.setCanceledOnTouchOutside(true);

        TextView text_cancel, text_confirm;

        final WheelView wheel_bank;

        text_cancel = (TextView) view.findViewById(R.id.text_cancel);
        text_confirm = (TextView) view.findViewById(R.id.text_confirm);
        wheel_bank = (WheelView) view.findViewById(R.id.wheel_bank);


        wheel_bank.setViewAdapter(mAdapter);
        text_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });

        text_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int i = wheel_bank.getCurrentItem();
                if (bankModelList.size() > 0) {
                    String bankName = bankModelList.get(i).getTwName();
                    twId = bankModelList.get(i).getTwId();
                    add_bank_name.setText(bankName);
                    bank = true;
                }
                dialog.dismiss();
            }
        });

        wheel_bank.addChangingListener(new OnWheelChangedListener() {
            @Override
            public void onChanged(WheelView wheel, int oldValue, int newValue) {

            }
        });

        wheel_bank.setVisibleItems(7);


        dialog.show();
    }

    private void loadBankList() {
        bankModelList = new ArrayList<>();
        RequestParams params = new RequestParams();

        params.addBodyParameter("twCode", "03");
        loadData(HttpRequest.HttpMethod.POST, ApiDefine.COMMON_DATA, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String response = responseInfo.result;
                try {
                    JSONObject object = new JSONObject(response);
                    Log.e(TAG, response);
                    String list = object.getString("list");
                    JSONArray listArray = new JSONArray(list);
                    for (int i = 0; i < listArray.length(); i++) {
                        JSONObject temp = listArray.getJSONObject(i);
                        String twId = temp.getString("twId");
                        String twCode = temp.getString("twCode");
                        String twName = temp.getString("twName");//银行卡类别名字
                        BankModel model = new BankModel(twId, twName);
                        bankModelList.add(model);
                    }
                    mAdapter = new ArrayWheelAdapter<List<BankModel>>(AddBankActivity.this, bankModelList);
                    mAdapter.notify();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 1) {
                finish();
            }
        }
    }

    /*@Override
    public void onPayStart(PayType payType) {

    }

    @Override
    public void onPaySuccessed(PayType payType, String s) {

    }

    @Override
    public void onPayFailed(PayType payType, String s) {

    }

    @Override
    public void onPayCanceled(PayType payType) {

    }

    @Override
    public void onPayConfirming(PayType payType) {

    }*/

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        cardNum = edit_card_id.getText().toString();
        if (TextUtils.isEmpty(cardNum)) {
            btn_next.setEnabled(false);
            btn_next.setTextColor(Color.parseColor("#dfafaf"));
        } else {
            btn_next.setEnabled(true);
            btn_next.setTextColor(Color.parseColor("#ffffff"));
        }
    }

    @Override
    public void afterTextChanged(Editable editable) {

    }
}
